<template>
  <div class="home-sidebar">
    <!--<el-menu unique-opened router>-->
    <el-menu :default-active="onRouterName" class="sidebar-el-menu" :collapse="collapse" unique-opened @select="handleSelect">
      <template v-for="item1 in menuItems">
        <template v-if="item1.children.length > 0">
          <el-submenu :index="item1.name" :key="item1.name">
            <template slot="title">
              <i v-if="item1.icon" :class="item1.icon"></i>
              <span>{{ item1.title }}</span>
            </template>
            <template v-for="item2 in item1.children">
              <el-submenu v-if="item2.children.length > 0" :index="item2.name" :key="item2.name">
                <template slot="title">
                  <i v-if="item2.icon" :class="item2.icon"></i>
                  <span>{{ item2.title }}</span>
                </template>
                <el-menu-item v-for="item3 in item2.children" :key="item3.name" :index="item3.name">
                  <i v-if="item2.icon" :class="item3.icon"></i>
                  <span>{{ item3.title }}</span>
                </el-menu-item>
              </el-submenu>
              <el-menu-item v-else :index="item2.name" :key="item2.name">
                <i v-if="item2.icon" :class="item2.icon"></i>
                <span>{{ item2.title }}</span>
              </el-menu-item>
            </template>
          </el-submenu>
        </template>
        <template v-else>
          <el-menu-item :index="item1.name" :key="item1.name">
            <i v-if="item1.icon" :class="item1.icon"></i>
            <span slot="title">{{ item1.title }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>
<script>
import bus from "@/utils/bus";

export default {
  data(){
    return {
      collapse: false,
      menuItems: [],
      tabsAmount: 0
    }
  },
  computed:{
    onRouterName() {
      return this.$route.name;
    }
  },
  methods:{
    getMenuData(){
      this.menuItems = this.$store.getters['menu/getAuthMenu']
    },
    handleSelect(key, keyPath) {
      // 标签栏超过10个则不允许再打开
      if(this.tabsAmount > 9){
        this.$message('页面标签栏数量超过10个，请先关闭部分页面!');
        return
      }
      this.$router.push({ name: key /*, params: { data: 'query' }*/})
    }
  },
  created() {
    bus.$on("collapse", msg => {
      this.collapse = msg;
    });

    bus.$on("tabs", msg => {
      this.tabsAmount = msg.length;
    });

    this.getMenuData();
  }
}
</script>
<style>
.home-sidebar{
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
}
.home-sidebar::-webkit-scrollbar {
  width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
  width: 250px;
}
.home-sidebar > ul {
  height: 100%;
}
</style>
